<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="600px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="100px">
      <el-form-item label="订单编号">
        <div>{{ form.orderNo }}</div>
      </el-form-item>
      <el-form-item label="实付金额">
        <div>￥{{ form.cost }} </div>
      </el-form-item>
      <el-form-item label="退款金额" prop="refundcost" required>
        <el-input-number
          v-model="form.refundcost"
          :max="form.cost"
          :precision="2"
          controls-position="right"
        >
        </el-input-number>
      </el-form-item>
      <el-form-item label="退款备注" prop="refundRemark">
        <el-input
          v-model.trim="form.refundRemark"
          placeholder="请输入退款备注"
          clearable
          :maxLength="30"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cancelOrder } from "@/api/service";
import { order_shifujinge } from '../components/mixin.js'
export default {
  data() {
    return {
      title: "取消订单并退款",
      open: false,
      form: {},
    }
  },
  methods: {
    order_shifujinge,
    openModal(row) {
      this.form = {
        id: row.id,
        orderNo: row.orderNo,
        cost: order_shifujinge(row),
        refundcost: order_shifujinge(row),
        refundRemark: ""
      }
      this.open = true;
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },

    submitForm() {
      const that = this;
      if(that.form.refundcost < 0){
        that.$message.error("退款金额不得为负数");
        return
      }
      if(that.form.refundcost > that.form.cost){
        that.$message.error("退款金额不得大于实付金额");
        return
      }
      that.$refs["form"].validate(async (valid) => {
        if (valid) {
          cancelOrder(that.form).then(res=>{
            that.$message.success(that.refundcost ? "取消订单并退款成功" : '取消订单成功');
            that.$emit("refresh");
            that.closeModal();
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
